package net.oschina.jhssm.common.util;

import net.oschina.jhssm.module.system.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * Created by zhaojb on 2017/8/21.
 */
public class ShiroUtil {

    private ShiroUtil() {

    }

    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static SysUser getUser() {
        if (isLogin()) {
            return (SysUser) SecurityUtils.getSubject().getPrincipal();
        } else {
            return null;
        }
    }

    public static String getUserId() {
        if (isLogin()) {
            return getUser().getId();
        } else {
            return "";
        }
    }

    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

}
